<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container {
            height: 400px;
            width: 100%;
            border-left: 3px solid red;
            border-top: 3px solid red;
        }
        .nav {
            height: 100%;
            width: 100px;
            float: left;
            box-sizing: border-box;
        }
        .nav-item {
            height: 25%;
            width: 100%;
            text-align: center;
            /* 文字纵向居中 start */
            display: flex;
            flex-direction: column;
            justify-content: center;
            /* 文字纵向居中 end */
            box-sizing: border-box;
            border-right: 3px solid red;
            border-bottom: 3px solid red;
        }
        .nav-item:hover {
            background-color: red;
        }
        .intro {
            height: 100%;
            /* calculate 计算 */
            width: calc(100% - 100px);
            border-right: 3px solid red;
            border-bottom: 3px solid red;
            float: left;
            box-sizing: border-box;
        }
    </style>
</head>
<body>

    <div class="container">
        <div class="nav">
            <!-- to do -->
        </div>
        <div class="intro"></div>
    </div>

    <script>

        const data = [
            {
                color: 'red',
                title: '女靴',
            },
            {
                color: 'aqua',
                title: '雪地靴',
            },
            {
                color: 'pink',
                title: '冬裙',
            },
            {
                color: 'blue',
                title: '呢大衣',
            },
            {
                color: 'black',
                title: 'xxxx',
            },
        ];

        // 写一个初始化函数
        // 把数组的每一项渲染到新建的dom元素中
        // function createListItems () {
        //     const father = document.querySelector('.nav');
        //     const intro = document.querySelector('.intro');
        //     for (const item of data) {
        //         // 创建元素
        //         const el = document.createElement('div');
        //         el.setAttribute('class', 'nav-item');
        //         // el.setAttribute('style', 'height: ' + 100 / data.length + '%');
        //         // es6拼接字符串
        //         el.setAttribute('style', `height: ${ 100 / data.length }%`);
        //         el.innerText = item.title;
        //         // 添加事件
        //         el.addEventListener('mouseover', function () {
        //             intro.setAttribute('style', `background: ${ item.color }`);
        //         });
        //         father.appendChild(el);
        //     }
        // }

        // createListItems();

        // for循环中如果使用var配合i++的方式，循环体中一旦有异步函数，就会有问题
        // 解决方案1：let
        // 解决方案2：把i放在函数作用域中
        function createListItems () {
            const father = document.querySelector('.nav');
            const intro = document.querySelector('.intro');
            for (var i = 0; i < data.length; i++) {
                // 创建元素
                const el = document.createElement('div');
                el.setAttribute('class', 'nav-item');
                // el.setAttribute('style', 'height: ' + 100 / data.length + '%');
                // es6拼接字符串
                el.setAttribute('style', `height: ${ 100 / data.length }%`);
                el.innerText = data[i].title;

                (function (i) { // 形式参数
                    // 添加事件
                    el.addEventListener('mouseover', function () {
                        console.log(i); // 6
                        intro.setAttribute('style', `background: ${ data[i].color }`);
                    });
                })(i); // 实际参数

                father.appendChild(el);
            }
        }

        createListItems();

        // 小扩展
        // 面试题***
        for (var i = 0; i < 3; i++) {
            console.log('a' + i);
            setTimeout(function () {
                console.log('b' + i);
                // i
                (function () {
                    console.log('c' + i);
                })(i);
            }, 1000 * i); // 0, 1, 2
        }

        // 0s: a0, a1, a2
        // 异步0s: b3, c3
        // 异步1s: b3, c3
        // 异步2s: b3, c3


    </script>
</body>
</html>